package com.css.dzj.dzjap.modules.lawenforcesuper.controller;

import com.alibaba.fastjson.JSON;
import com.css.aq.server.service.BaseService;
import com.css.dzj.dzjap.common.audit.service.SysAuditService;
import com.css.dzj.dzjap.common.util.Constant;
import com.css.dzj.dzjap.common.util.R;
import com.css.dzj.dzjap.modules.apunit.entity.ApUnitEntity;
import com.css.dzj.dzjap.modules.lawenforcesuper.entity.ApDeptExtractInfoEntity;
import com.css.dzj.dzjap.modules.lawenforcesuper.entity.ApSupersiveTaskInfoEntity;
import com.css.dzj.dzjap.modules.lawenforcesuper.service.ApDeptExtractService;
import com.css.dzj.dzjap.modules.lawenforcesuper.service.SuperTaskService;
import com.css.dzj.dzjap.modules.publick.entity.ApPublicEntity;
import com.css.dzj.dzjap.modules.record.approject.entity.ApRecordEntity;
import com.css.dzj.dzjap.modules.record.approject.service.ApRecordService;
import com.css.dzj.dzjap.modules.record.community.entity.ApRecordGainEntity;
import com.css.dzj.dzjap.modules.record.community.service.CommunityRecordService;
import com.querydsl.core.QueryResults;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 项目抽取抽取controller
 *
 * @author limanman
 */
@RequestMapping("/supersiveController")
@RestController
public class SuperController {
    @Autowired
    ApRecordService apRecordService;
    @Autowired
    CommunityRecordService communityRecordService;
    @Autowired
    SuperTaskService superTaskService;
    @Autowired
    ApDeptExtractService apDeptExtractService;
    @Autowired
    private SysAuditService sysAuditService;
    @Autowired
    HttpServletRequest request;
    @PostMapping("/saveTaskInfo/{type}")
    public R saveSuper(@PathVariable String type, @RequestBody ApSupersiveTaskInfoEntity apSupersiveTaskInfoEntity, HttpServletRequest request) {
        String s = "";
        try {
            R r = superTaskService.saveTaskInfo(type, apSupersiveTaskInfoEntity, request);

            if ("500".equals(r.get("code").toString())) {
                return R.error(r.get("msg").toString());
            }
            s = JSON.toJSONString(apSupersiveTaskInfoEntity);
            sysAuditService.saveSuccessAudit("添加成功", Constant.SUPERSIVECONTROLLER, Constant.PT, Constant.ADD, s, request);
            return R.ok(r).put("taskId", r.get("taskId"));
        } catch (Exception e) {
            e.printStackTrace();
            s = JSON.toJSONString(null);
            sysAuditService.saveFailAudit("添加失败", Constant.SUPERSIVECONTROLLER, Constant.YZ, Constant.ADD, s, e, request);
            return R.error(e.getMessage());
        }
    }

    /**
     * 根据标题和类型查询监管任务
     *
     * @param taskTitle
     * @param type
     * @param pageNo
     * @param pageSize
     * @return
     */
    @GetMapping("/queryApSupersiveTaskInfo")
    public R queryApSupersiveTaskInfo(@RequestParam(name = "taskTitle", required = false) String taskTitle,
                                      @RequestParam(name = "type", required = false) String type,
                                      @RequestParam(name = "pageNo", required = false, defaultValue = "1") Integer pageNo,
                                      @RequestParam(name = "pageSize", required = false, defaultValue = "10") Integer pageSize, HttpServletRequest request) {
        String s = "";
        try {
            PageRequest pageRequest = PageRequest.of(pageNo - 1, pageSize);
            QueryResults<ApSupersiveTaskInfoEntity> apSupersiveTaskInfoEntities = superTaskService.queryApSupersiveTaskInfo(pageRequest, taskTitle, type, request);
            s = JSON.toJSONString(apSupersiveTaskInfoEntities);
            sysAuditService.saveSuccessAudit("信息查询成功", Constant.SUPERSIVECONTROLLER, Constant.PT, Constant.SELECT, s, request);
            return R.ok().put("apSupersiveTaskInfoEntities", apSupersiveTaskInfoEntities);
        } catch (Exception e) {
            e.printStackTrace();
            s = JSON.toJSONString(null);
            sysAuditService.saveFailAudit("信息查询失败", Constant.SUPERSIVECONTROLLER, Constant.YZ, Constant.SELECT, s, e, request);
            return R.error(e.getMessage());
        }
    }

    /**
     * 查询抽取的项目
     *
     * @param taskId
     * @param pageNo
     * @param pageSize
     * @return
     */
    @PostMapping("/queryApRecord")
    public R queryApRecord(@RequestParam(name = "taskId", required = false) String taskId,
                           @RequestParam(name = "pageNo", required = false, defaultValue = "1") Integer pageNo,
                           @RequestParam(name = "pageSize", required = false, defaultValue = "10") Integer pageSize, HttpServletRequest request) {
        String s = "";
        try {
            PageRequest pageRequest = PageRequest.of(pageNo - 1, pageSize);
            QueryResults apRecords = superTaskService.queryApRecord(pageRequest, taskId, request);
            ApSupersiveTaskInfoEntity apSupersiveTaskInfoEntity = superTaskService.queryApSupersiveTaskInfoById(taskId);
            //TODO
            s = JSON.toJSONString(apSupersiveTaskInfoEntity);
            sysAuditService.saveSuccessAudit("信息查询成功", Constant.SUPERSIVECONTROLLER, Constant.PT, Constant.SELECT, s, request);
            return R.ok().put("apRecords", apRecords).put("apSupersiveTaskInfoEntity", apSupersiveTaskInfoEntity);
        } catch (Exception e) {
            e.printStackTrace();
            s = JSON.toJSONString(null);
            sysAuditService.saveFailAudit("信息查询失败", Constant.SUPERSIVECONTROLLER, Constant.YZ, Constant.SELECT, s, e, request);
            return R.error(e.getMessage());
        }

    }

    /***
     * 拟制公告
     * @param apPublicEntity
     * @return
     */
    @PostMapping("/addPub")
    public R addPub(@RequestBody ApPublicEntity apPublicEntity, HttpServletRequest request) {
        String s = "";
        try {
            if (StringUtils.isEmpty(apPublicEntity)) {
                return R.error("公告为空");
            }
            superTaskService.addPulic(apPublicEntity, request);
            s = JSON.toJSONString(apPublicEntity);
            sysAuditService.saveSuccessAudit("添加成功", Constant.SUPERSIVECONTROLLER, Constant.PT, Constant.ADD, s, request);
            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();
            s = JSON.toJSONString(null);
            sysAuditService.saveFailAudit("添加失败", Constant.SUPERSIVECONTROLLER, Constant.YZ, Constant.ADD, s, e, request);
            return R.error(e.getMessage());
        }
    }

    /***
     * 根据任务ID查找任务
     * @param taskId
     * @return
     */
    @PostMapping("/getById")
    public R findById(@RequestParam(name = "id") String taskId) {
        String s = "";
        try{
            if (!StringUtils.isEmpty(taskId)) {
                ApSupersiveTaskInfoEntity apSupersiveTaskInfoEntity = superTaskService.findById(taskId);
                s = JSON.toJSONString(apSupersiveTaskInfoEntity);
                sysAuditService.saveSuccessAudit("查询成功", Constant.SUPERSIVECONTROLLER, Constant.PT, Constant.SELECT, s, request);
                return R.ok().put("data", apSupersiveTaskInfoEntity);
             }
             return R.ok();
        }catch (Exception e){
            e.printStackTrace();
            s = JSON.toJSONString(null);
            sysAuditService.saveFailAudit("信息查询失败", Constant.SUPERSIVECONTROLLER, Constant.YZ, Constant.SELECT, s, e, request);
            return R.error(e.getMessage());
            }
    }

    /***
     * 根据任务类型查询任务
     * @param type
     * @param pageNo
     * @param pageSize
     * @param taskTitle
     * @param request
     * @return
     */
    @GetMapping("/getTask")
    public R findTaskByType(@RequestParam(name = "type") String type,
                            @RequestParam(name = "pageNo", required = false, defaultValue = "1") Integer pageNo,
                            @RequestParam(name = "pageSize", required = false, defaultValue = "10") Integer pageSize,
                            @RequestParam(name = "taskTitle", required = false) String taskTitle,
                            HttpServletRequest request) {
        String s = "";
        try{
        if (!StringUtils.isEmpty(type)) {
            /***
             * 判断类型
             */
            if ("4".equals(type)) {
                PageRequest pageable = PageRequest.of(pageNo - 1, pageSize);
                QueryResults<ApDeptExtractInfoEntity> listAp = apDeptExtractService.queryAllExtract(pageable, taskTitle, request);
                s = JSON.toJSONString(listAp);
                sysAuditService.saveSuccessAudit("查询成功", Constant.SUPERSIVECONTROLLER, Constant.PT, Constant.SELECT, s, request);
                return R.ok().put("data", listAp);
            } else if ("0".equals(type) || "1".equals(type) || "2".equals(type) || "3".equals(type)) {
                if ("0".equals(type) || "1".equals(type) || "2".equals(type)) {
                    PageRequest pageable = PageRequest.of(pageNo - 1, pageSize);
                    QueryResults<ApSupersiveTaskInfoEntity> list = superTaskService.queryAllApSupersiveTaskInfo(pageable, taskTitle, type, request);
                    s = JSON.toJSONString(list);
                    sysAuditService.saveSuccessAudit("查询成功", Constant.SUPERSIVECONTROLLER, Constant.PT, Constant.SELECT, s, request);
                    return R.ok().put("data", list);
                } else {
                    PageRequest pageable = PageRequest.of(pageNo -1, pageSize);
                    QueryResults<ApRecordEntity> listSH = apRecordService.findAll(pageable);
                    s = JSON.toJSONString(listSH);
                    sysAuditService.saveSuccessAudit("查询成功", Constant.SUPERSIVECONTROLLER, Constant.PT, Constant.SELECT, s, request);
                    return R.ok().put("data", listSH);
                }
            }
        }
                sysAuditService.saveSuccessAudit("查询成功", Constant.SUPERSIVECONTROLLER, Constant.PT, Constant.SELECT, s, request);
                return R.ok();
        }catch(Exception e) {
                 e.printStackTrace();
                s = JSON.toJSONString(null);
                sysAuditService.saveFailAudit("信息查询失败", Constant.SUPERSIVECONTROLLER, Constant.YZ, Constant.SELECT, s, null, request);
                return R.error("请选择监管类别");
            }

    }

    /***
     * 根据任务类型和任务ID查询项目
     * @param type
     * @param taskId
     * @return
     */
    @GetMapping("/getpro")
    public R findProByTaskId(String type, String taskId) {
        String s = "";
        try {
            if (StringUtils.isEmpty(taskId) || StringUtils.isEmpty(type)) {
                return R.error("类型和ID不能为空");
            }
            if ("4".equals(type)) {
                List<ApUnitEntity> listAP = apDeptExtractService.findDeptByTaskId(taskId);
                if (!StringUtils.isEmpty(listAP) && listAP.size() > 0) {
                    s = JSON.toJSONString(listAP);
                    sysAuditService.saveSuccessAudit("查询成功", Constant.SUPERSIVECONTROLLER, Constant.PT, Constant.SELECT, s, request);
                    return R.ok().put("data", listAP);
                }
            } else if ("0".equals(type) || "1".equals(type) || "2".equals(type) || "3".equals(type)) {
                if ("0".equals(type) || "1".equals(type) || "2".equals(type)) {
                    if ("2".equals(type)) {
                        List<ApRecordEntity> listRe = superTaskService.findRecordById(taskId);
                        if (!StringUtils.isEmpty(listRe) && listRe.size() > 0) {
                            s = JSON.toJSONString(listRe);
                            sysAuditService.saveSuccessAudit("查询成功", Constant.SUPERSIVECONTROLLER, Constant.PT, Constant.SELECT, s, request);
                            return R.ok().put("data", listRe);
                        }
                    } else {
                        List<ApRecordGainEntity> listReG = superTaskService.finRecorGById(taskId);
                        if (!StringUtils.isEmpty(listReG) && listReG.size() > 0) {
                            s = JSON.toJSONString(listReG);
                            sysAuditService.saveSuccessAudit("查询成功", Constant.SUPERSIVECONTROLLER, Constant.PT, Constant.SELECT, s, request);
                            return R.ok().put("data", listReG);
                        }
                    }
                }
            }
            sysAuditService.saveSuccessAudit("查询成功", Constant.SUPERSIVECONTROLLER, Constant.PT, Constant.SELECT, s, request);
            return R.ok();
        }catch (Exception e){
            e.printStackTrace();
            s = JSON.toJSONString(null);
            sysAuditService.saveFailAudit("信息查询失败", Constant.SUPERSIVECONTROLLER, Constant.YZ, Constant.SELECT, s, e, request);
            return R.error(e.getMessage());
        }

    }
    /**
     * 根据任务id查询抽取的项目ApRecordEntity
     * @param taskId
     * @return
     */
    @PostMapping("/findRecordById")
    public R findRecordById(String taskId){
        String s ="";
        try {
            if (StringUtils.isEmpty(taskId)) {
                return R.error("任务ID不能为空");
            }
            List<ApRecordEntity> list = superTaskService.findRecordById(taskId);
            s = JSON.toJSONString(list);
            sysAuditService.saveSuccessAudit("查询成功", Constant.SUPERSIVECONTROLLER, Constant.PT, Constant.SELECT, s, request);
            return R.ok().put("data", list);
        }catch (Exception e){
            e.printStackTrace();
            s = JSON.toJSONString(null);
            sysAuditService.saveFailAudit("查询失败", Constant.SUPERSIVECONTROLLER, Constant.PT, Constant.SELECT, s, e, request);
            return R.error(e.getMessage());
        }
    }

    /**
     * 根据任务id查询抽取的项目ApRecordGainEntity
     * @param taskId
     * @return
     */
    @PostMapping("/findRecordGById")
    public R findRecordGById(String taskId){
        String s = "";
        try {
            if (StringUtils.isEmpty(taskId)) {
                return R.error("任务ID不能为空");
            }
            List<ApRecordGainEntity> list = superTaskService.finRecorGById(taskId);
            s = JSON.toJSONString(list);
            sysAuditService.saveSuccessAudit("查询成功", Constant.SUPERSIVECONTROLLER, Constant.PT, Constant.SELECT, s, request);
            return R.ok().put("data", list);
        }catch (Exception e){
            e.printStackTrace();
            s = JSON.toJSONString(null);
            sysAuditService.saveFailAudit("查询失败", Constant.SUPERSIVECONTROLLER, Constant.PT, Constant.SELECT, s, e, request);
            return R.error(e.getMessage());
        }
    }

}
